package com.cntcdt.api.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cntcdt.api.db.dao.base.BuyHistoryDaoBase;
import com.cntcdt.api.db.model.BuyHistoryModel;
import com.cntcdt.api.db.model.CheckHistoryModel;
import com.cntcdt.api.db.model.DeviceModel;

/**
 * 这是cdt_buy_history的Dao实现类
 */
public class BuyHistoryDao extends BuyHistoryDaoBase {
	/**
	 * BuyHistoryDao对象
	 */
	private static BuyHistoryDao instance = new BuyHistoryDao();

	/**
	 * 默认构造方法
	 */
	private BuyHistoryDao() {
	}

	/**
	 * 获得对象实例
	 */
	public static BuyHistoryDao getInstance() {
		return instance;
	}

	/**
	 * 查询出下雨sortTime的limit条记录
	 * 
	 * @param userId
	 * @param sortTime
	 * @param limit
	 * @return
	 */
	public List<BuyHistoryModel> findBySortTime(Integer userId, Long sortTime, int limit) {
		String sql = "SELECT * FROM " + TABLE
				+ " WHERE userId = ? and sortTime<? and status = 1 order by sortTime desc limit ?";
		Object[] paramObjs = { userId, sortTime, limit };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	// public List<>

	/**
	 * 微信公众号账号绑定旧账号,更新所有记录的userid 获得model集合
	 * 
	 * @return 返回Model集合
	 */
	public boolean bindWXUpdate(int userId, int userIdOld) {
		String sql = "update " + TABLE + " set userIdOld=? , userId=? where userId=? ";
		try {
			int re = writeDBEngine.executeUpdate(sql, new Object[] { userIdOld, userId, userIdOld });
			if (re > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 根据设备号查询前面两条
	 * 
	 * @param userId
	 * @param sortTime
	 * @param limit
	 * @return
	 */
	public List<BuyHistoryModel> findByTerminal(String terminal) {
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? order by sortTime desc limit 3";
		Object[] paramObjs = { terminal };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<BuyHistoryModel> findByTerminalIn24H(String terminal) {
		long sortTime = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? and sortTime < ? order by sortTime desc limit 3";
		Object[] paramObjs = { terminal, sortTime };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<BuyHistoryModel> findByTimeStrAndSortTime(String terminal, Date yesterdayTime, Date todayTime,
			long sortTime, String terminalList, String noSearchUserId) {
		List<BuyHistoryModel> modelList = new ArrayList<BuyHistoryModel>();
		if (terminalList.equals("")) {
			String sql = "SELECT * FROM " + TABLE
					+ " where terminal=? and addTime >=? and addTime<? and sortTime < ? and userId not in ("
					+ noSearchUserId + ") and status = 1  order by sortTime desc limit 20";
			modelList = queryModelList(sql, new Object[] { terminal, yesterdayTime, todayTime, sortTime });
		} else {
			String sql = "SELECT * FROM " + TABLE + " where terminal in (" + terminalList
					+ ") and addTime >=? and addTime<? and sortTime < ?  and userId not in (" + noSearchUserId
					+ ") and status = 1 order by sortTime desc limit 20";
			modelList = queryModelList(sql, new Object[] { yesterdayTime, todayTime, sortTime });
		}
		return modelList;
	}

	/**
	 * 查询出售线失败的记录
	 * 
	 * @param terminal
	 * @return
	 */
	public List<BuyHistoryModel> findByFailHistory(String terminal, int sortTime) {
		sortTime = sortTime * 20;
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? and status = 0 order by addTime desc limit ?,20";
		Object[] paramObjs = { terminal, sortTime };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 查询出售线失败的记录
	 * 
	 * @param terminal
	 * @return
	 */
	public List<BuyHistoryModel> findByAllHistory(String terminal, int sortTime) {
		sortTime = sortTime * 20;
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? order by addTime desc limit ?,20";
		Object[] paramObjs = { terminal, sortTime };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 查找出24小时内售线失败次数
	 * 
	 * @param terminals
	 * @return
	 */
	public List<BuyHistoryModel> findSellLineFailDeviceNum(String terminals) {
		String sql = "select * from " + TABLE + " where addTime <= ? and addTime >= ? and status = 0  and terminal in ("
				+ terminals + ")";
		Object[] paramObjs = { new Date(), new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000) };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 根据用户id查询购线记录
	 * 
	 * @param userId
	 * @param sortTime
	 * @return
	 */
	public List<BuyHistoryModel> findSellLineHistoryByUserId(int userId, int sortTime) {
		sortTime = sortTime * 20;
		String sql = "select * from " + TABLE + " where userId = ? and type = 1 order by addTime desc limit ?,20";
		Object[] paramObjs = { userId, sortTime };
		List<BuyHistoryModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

}
